﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.MediaCenter.Hosting;
using RMC.Commands;

namespace RMC
{
	internal class Command
	{
		private static Dictionary<CommandResultType, string> messages = new Dictionary<CommandResultType, string>() 
		{ 
			{ CommandResultType.Executed, "200 Ok." },
			{ CommandResultType.ExecutedWithContent, "201 Ok." },
			//{ CommandResultType.NotFound, "400 Method not found." },
			{ CommandResultType.BadArguments, "401 Bad arguments." },
			{ CommandResultType.Exception, "500 Exception." }
		};

		internal static Dictionary<string, ICommand> list = new Dictionary<string, ICommand> 
		{ 
		  { "help", new HelpCommand() },
			{ "button", new ButtonCommand() },
			{ "goto", new GotoCommand() },
		  { "volume", new VolumeCommand() }
		};

		public static string Execute(string command)
		{
			string args = "";

			if (command.IndexOf(' ') != -1)
			{
				string[] array = command.Split(new char[] { ' ' }, 2);
				command = array[0];
				args = array[1];
			}

			ICommand c;
			if (list.TryGetValue(command.ToLower(), out c))
			{
				CommandResult rc = c.Execute(args);
				string s = messages[rc.Type] + "\r\n";

				if (rc.Type == CommandResultType.ExecutedWithContent || rc.Type == CommandResultType.Exception)
				{
					s += rc.Content;
					if(!rc.Content.EndsWith("\r\n"))
						s += "\r\n";
				}


				return s;
			}
			
			return "400 Method not found.\r\n";
		}
	}
}
